package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.ParkPointBoundDao;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.ParkPointBound;
import com.tbit.main.pojo.ParkPointBoundCount;
import com.tbit.main.service.ParkPointBoundService;
import com.tbit.main.service.ParkPointService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author LMY
 * @create 2021-12-22 16:25
 */
@Service("parkPointBoundService")
public class ParkPointBoundServiceImpl implements ParkPointBoundService {

    @Autowired
    private ParkPointBoundDao parkPointBoundDao;
    @Autowired
    private ParkPointService parkPointService;

    @Override
    public List<ParkPointBoundCount> getByAccountId(List<Integer> parkPointIds, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("parkPointIds", StringUtil.getListString(parkPointIds));
        map.put("type", type);
        return parkPointBoundDao.getByAccountIdGroup(map);
    }

    @Override
    public List<ParkPointBound> getByParkPointId(Integer parkPointId, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("parkPointId", parkPointId);
        map.put("type", type);
        return parkPointBoundDao.getByParkPointId(map);
    }

    @Override
    public void updateTime(List<String> machineNOs, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineNOs", StringUtil.getStringListString(machineNOs));
        map.put("type", type);
        parkPointBoundDao.updateTime(map);
    }

    @Override
    public void add(ParkPointBound parkPointBound) {
        parkPointBoundDao.add(parkPointBound);
    }

    @Override
    public void del(String machineNO) {
        parkPointBoundDao.del(machineNO);
    }

    @Override
    public void update(String machineNO, Integer type, Integer state, String remark) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineNO", machineNO);
        map.put("type", type);
        map.put("state", state);
        map.put("remark", remark);
        parkPointBoundDao.update(map);
    }

    @Override
    public ParkPointBound getByNO(String machineNO, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineNO", machineNO);
        map.put("type", type);
        return parkPointBoundDao.getByNO(map);
    }

    @Override
    public List<ParkPointBound> getByNOTime(List<String> machineNOs, String time) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineNOs", StringUtil.getStringListString(machineNOs));
        map.put("time", time);
        return parkPointBoundDao.getByNOTime(map);
    }

    @Override
    public void updateState(List<String> machineNOs, Integer state) {
        parkPointBoundDao.updateState(StringUtil.getStringListString(machineNOs), state);
    }

    @Override
    public List<ParkPointBound> getTime(String time, Integer type, List<Integer> parkPointId, Integer state, Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("time", time);
        map.put("parkPointIds", StringUtil.getListString(parkPointId));
        map.put("state", state);
        map.put(DaoParamConstant.accountId, accountId);
        return parkPointBoundDao.getTime(map);
    }

    @Override
    public List<ParkPointBoundCount> getTimeCount(String time, Integer type, List<Integer> parkPointId) {
        Map<String, Object> map = new HashMap<>();
        map.put("time", time);
        map.put("type", type);
        map.put("parkPointIds", StringUtil.getListString(parkPointId));
        return parkPointBoundDao.getTimeCount(map);
    }

    @Override
    public List<ParkPointBoundCount> getByStateId(Integer state, Integer type, List<Integer> parkPointId) {
        Map<String, Object> map = new HashMap<>();
        map.put("state", state);
        map.put("parkPointId", StringUtil.getListString(parkPointId));
        return parkPointBoundDao.getByStateId(map);
    }

    @Override
    public Set<Integer> getByParkPointIdAll() {
        return parkPointBoundDao.getByParkPointIdAll();
    }

    @Override
    public void addBatch(String machineNOs, Integer parkPointId, Integer type) {
        List<String> nos = Arrays.asList(machineNOs.split(","));
        List<String> existNos = getByMachineNOs(nos, type);
        if (!existNos.isEmpty()) {
            throw new BaseException("ParkPoint.is.exist");
        }
        /*ParkPoint parkPoint= parkPointService.getByParkPointId(parkPointId);
        if(parkPoint==null){
            throw new BaseException("ParkPoint.notExist");
        }*/
        List<ParkPointBound> list = new ArrayList<>();
        String date = DateTimeUtil.getNowTime();
        /**封装数据*/
        for (String machineNO : nos) {
            ParkPointBound parkPointBound = new ParkPointBound();
            parkPointBound.setMachineNO(machineNO);
            parkPointBound.setParkPointId(parkPointId);
            parkPointBound.setType(type);
            parkPointBound.setState(0);
            parkPointBound.setAddTime(date);
            list.add(parkPointBound);
        }
        parkPointBoundDao.addBatch(list);
    }

    @Override
    public List<String> getByMachineNOs(List<String> machineNOs, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineNOs", StringUtil.getStringListString(machineNOs));
        map.put("type", type);
        return parkPointBoundDao.getByMachineNOs(map);
    }

    @Override
    public void delBatch(List<String> machineNOs, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineNOs", StringUtil.getStringListString(machineNOs));
        map.put("type", type);
        parkPointBoundDao.delBatch(map);
    }
}
